<?php namespace Common\Model;
use Hdphp\Model\Model;
class User extends Model{
	//指定表名
	protected $table='user';
	//自动完成
	protected $auto=[
		['password','md5','function',3,3]
	];
	//自动验证
	protected $validate=[
		['code','required','验证码不能为空',3,3],
		['code','_code','验证码输入错误',3,3],
		['username','required','用户名不能为空',3,3],
		['password','required','密码不能为空',3,3],
		['username','_username','用户名或密码输入错误',3,3],		
		['password','_password','用户名或密码输入错误',3,3]		
	];
	//自定义验证
	public function _code(){
	    $code=Q('post.code');
		if(strtolower($code)!=strtolower($_SESSION['code'])){
			return FALSE;
		}
		return TRUE;
	}
	public function _username(){
		$username=Q('post.username');
		$username=$this->where("username='{$username}'")->find();
		if($username){
			return TRUE;
		}
		return FALSE;
	}
	public function _password(){
		$username=Q('post.username');
		$password=Q('post.password','','md5');
		$username=$this->where("username='{$username}'")->pluck('password');
		if($password!=$username){
			return FALSE;
		}
		return TRUE;
	}
	//登录
	public function store(){
        if(!$this->create()) {
        	return FALSE;
		}else{
			$username=Q('post.username');
			$username=$this->where("username='{$username}'")->find();
			$_SESSION['nickuser']=[
				'uid'=>$username['uid'],
				'nickname'=>$username['nickname'],
				'photo'=>$username['photo'],
				'personalized'=>$username['personalized']
			];
			return TRUE;
		}
    }
	
}
 ?>